/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author DELL
 * @create 2018/8/6
 */

package com.guopiao.vat.cronjob.job;

import com.guopiao.vat.common.model.TjMonthReport;
import com.guopiao.vat.common.util.DateTimeUtil;
import com.guopiao.vat.common.util.DateUtil;
import com.guopiao.vat.service.MonthReportSkService;
import com.guopiao.vat.service.TjMonthReportService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.*;

@Component
public class MonthReportJob implements BaseJob{

    private  static Logger _log = LoggerFactory.getLogger(MonthReportJob.class);

    @Autowired
    private MonthReportSkService skService;

    @Autowired
    private TjMonthReportService reportService;

    public static MonthReportJob monthReportJob;

    public MonthReportJob() {

    }

    @PostConstruct
    public void init() {
        monthReportJob = this;
        //monthReportJob.reportService = reportService;
        //monthReportJob.skService = skService;
    }



    public void execute(JobExecutionContext context) throws JobExecutionException {

        _log.info("月度报表定时任务开始。。。。。。。。。。。。。");
        //获取当前时间的前一天
         Date d = DateTimeUtil.getQian(5,-1);
        String d1 = DateUtil.formatDate(d,DateUtil.DATE_FORMAT_YYYYMMDD);
        String ssnd = d1.substring(0,4);
        String ssyf = d1.substring(4,6);
        String date_begin = d1+"000000";
        String date_end = d1+"235959";

        Map<String,String> reqMap = new HashMap<String, String>();
        reqMap.put("date_begin",date_begin);
        reqMap.put("date_end",date_end);
        List<Map<String, Object>> nsr_list = monthReportJob.skService.getNsrxxList();
        if(!CollectionUtils.isEmpty(nsr_list)){
            String kpdwdm = "";
            String kpdwmc = "";
            for(Map<String , Object> map : nsr_list){
                 kpdwdm = map.get("kpdwdm").toString();
                 kpdwmc = map.get("kpdwmc").toString();
                reqMap.put("kpdwdm",kpdwdm);

                List<Map<String, Object>> report_list = monthReportJob.skService.monthReportCx(reqMap);
                if(!CollectionUtils.isEmpty(report_list)){
                    for(Map<String, Object> report_map : report_list){
                        String fpzt = report_map.get("fpzt").toString();
                        String sl = report_map.get("sl").toString();
                        String zje = report_map.get("zje").toString();
                        String zse = report_map.get("zse").toString();
                        String jqbh = report_map.get("jqbh").toString();
                        String fpfs = report_map.get("fpfs").toString();

                        Map<String,String>  ydbbMap = new HashMap<String, String>();
                        TjMonthReport reportModel = new TjMonthReport();
                        reportModel.setId(UUID.randomUUID().toString().replaceAll("-",""));
                        reportModel.setKpdwdm(kpdwdm);
                        reportModel.setKpdwmc(kpdwmc);
                        reportModel.setJqbh(jqbh);
                        reportModel.setZfbz(fpzt);
                       /* long a = Long.valueOf(zje);
                        reportModel.setZje(a);
                        reportModel.setZse(Long.valueOf(zse));*/
                        reportModel.setSl(sl);
                        reportModel.setSsnd(ssnd);
                        reportModel.setSsyf(ssyf);
                        reportModel.setFpfs(fpfs);
                        reportModel.setCreateTime(new Date());
                        //TODO 执行sql  插入数据
                        monthReportJob.reportService.insert(reportModel);

                    }
                }else{
                    _log.info("月度报表定时任务:"+kpdwdm+"--"+kpdwmc+"不存在数据");
                }
            }
        }else{
            _log.info("月度报表定时任务不存在需要执行的税号");
        }
        _log.info("月度报表定时任务结束。。。。。。。。。。。。。");

    }

    public MonthReportSkService getSkService() {
        return skService;
    }

    public void setSkService(MonthReportSkService skService) {
        this.skService = skService;
    }

    public TjMonthReportService getReportService() {
        return reportService;
    }

    public void setReportService(TjMonthReportService reportService) {
        this.reportService = reportService;
    }

    public static void main(String[] args) {
        String a = "0.00000000000000017";
        long b = Long.parseLong(a);
        System.out.println(b);
    }
}
